

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/egg.png">
  <link rel="icon" href="/img/egg.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="1、什么是 JavaScript相关链接：https:&#x2F;&#x2F;baike.baidu.com&#x2F;item&#x2F;JavaScript&#x2F;321142 Java Script （简称：JS）是一门跨平台、面向对象的脚本语言，是用来控制网页行为的，它使得网页可交互。 1234关于JavaScript有必要知道的事:1) JavaScript 和 Java 是完全不同的语言，不论是概念还是设计，但是基础语法类似。2)">
<meta property="og:type" content="article">
<meta property="og:title" content="前端基础学习2 | JavaScript">
<meta property="og:url" content="http://example.com/2024/03/05/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E5%AD%A6%E4%B9%A02-JavaScript/index.html">
<meta property="og:site_name" content="十一的蛋炒饭的博客">
<meta property="og:description" content="1、什么是 JavaScript相关链接：https:&#x2F;&#x2F;baike.baidu.com&#x2F;item&#x2F;JavaScript&#x2F;321142 Java Script （简称：JS）是一门跨平台、面向对象的脚本语言，是用来控制网页行为的，它使得网页可交互。 1234关于JavaScript有必要知道的事:1) JavaScript 和 Java 是完全不同的语言，不论是概念还是设计，但是基础语法类似。2)">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://uni-pic-bed.oss-cn-hangzhou.aliyuncs.com/blog2024/202403051913427.png">
<meta property="article:published_time" content="2024-03-05T11:28:38.000Z">
<meta property="article:modified_time" content="2024-03-05T11:28:43.775Z">
<meta property="article:author" content="十一的蛋炒饭">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://uni-pic-bed.oss-cn-hangzhou.aliyuncs.com/blog2024/202403051913427.png">
  
  
  
  <title>前端基础学习2 | JavaScript - 十一的蛋炒饭的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.7","typing":{"enable":true,"typeSpeed":60,"cursorChar":"","loop":false,"scope":["home"]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":"§"},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 7.1.1"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>十一的蛋炒饭 的博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/win7.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle">前端基础学习2 | JavaScript</span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2024-03-05 19:28" pubdate>
          2024年3月5日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          2.8k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          24 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar category-bar" style="margin-right: -1rem">
    





<div class="category-list">
  
  
</div>


  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">前端基础学习2 | JavaScript</h1>
            
              <p id="updated-time" class="note note-info" style="display: none">
                
                  
                    本文最后更新于 2024-03-05T19:28:43+08:00
                  
                  

                
              </p>
            
            
              <div class="markdown-body">
                
                <h2 id="1、什么是-JavaScript"><a href="#1、什么是-JavaScript" class="headerlink" title="1、什么是 JavaScript"></a>1、什么是 JavaScript</h2><p>相关链接：<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/JavaScript/321142">https://baike.baidu.com/item/JavaScript/321142</a></p>
<p>Java Script （简称：JS）是一门跨平台、面向对象的<strong>脚本语言</strong>，是用来控制网页行为的，它使得网页可交互。</p>
<figure class="highlight mipsasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs mipsasm">关于<span class="hljs-keyword">JavaScript有必要知道的事:</span><br><span class="hljs-keyword"></span><span class="hljs-number">1</span>) <span class="hljs-keyword">JavaScript </span>和 <span class="hljs-keyword">Java </span>是完全不同的语言，不论是概念还是设计，但是基础语法类似。<br><span class="hljs-number">2</span>) <span class="hljs-keyword">JavaScript </span>在 <span class="hljs-number">1995</span> 年由 Netscape公司的<span class="hljs-keyword">Brendan </span><span class="hljs-keyword">Eich </span>发明，并于 <span class="hljs-number">1997</span> 年成为ECMA标准。<br><span class="hljs-number">3</span>) ECMAScript6 （简称：ES6）是最常用的 <span class="hljs-keyword">JavaScript </span>版本（发布于 <span class="hljs-number">2015</span>年）。<br></code></pre></td></tr></table></figure>

<p>ECMA ：ECMA国际（前身为欧洲计算机制造商协会），制定了标准化的脚本程序设计语言 ECMAScript，这种语言得到广泛应用。而 JavaScript 是遵守 ECMAScript。</p>
<h2 id="2、-JavaScript-引入方式"><a href="#2、-JavaScript-引入方式" class="headerlink" title="2、 JavaScript 引入方式"></a>2、 JavaScript 引入方式</h2><p>JavaScript 引入方式一般有两种：内部引入和外部引入。</p>
<ol>
<li><p><strong>内部脚本引入</strong>：将 JS代码定义在 HTML 页面中。</p>
<ul>
<li>JavaScript代码必须位于<code>&lt;script&gt;&lt;/script&gt;</code> 标签之间。</li>
<li>在 HTML 文档中，可以在任意地方，放置任意数量的<code>&lt;script&gt;</code>。</li>
<li>一般把脚本置于 <code>&lt;body&gt;</code> 元素的底部，加载速度更快。</li>
</ul>
</li>
<li><p><strong>外部脚本引入</strong>：将 JS代码定义在外部 JS 文件中，然后引入到 HTML 页面中。</p>
<ul>
<li>外部文件中，只包含 JS 代码，无需使用 HTML 的 <code>&lt;script&gt;</code> 标签。</li>
<li><code>&lt;script&gt;</code> 标签不能自闭合，不支持<code>&lt;script/&gt;</code> 这种操作。</li>
</ul>
</li>
</ol>
<h2 id="3、JS-基础语法"><a href="#3、JS-基础语法" class="headerlink" title="3、JS 基础语法"></a>3、JS 基础语法</h2><h3 id="3-1-书写语法"><a href="#3-1-书写语法" class="headerlink" title="3.1 书写语法"></a>3.1 书写语法</h3><ul>
<li><p>区分大小写，与 Java 一样，变量名、函数名等都是区分大小写的。</p>
</li>
<li><p>每行结尾的分号 <code>;</code> 可有可无。</p>
</li>
<li><p>注释：</p>
<ul>
<li>单行注释： <code>// 注释内容 </code></li>
<li>多行注释：<code>/* 注释内容 */</code></li>
</ul>
</li>
<li><p>大括号表示代码块</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 判断</span><br><span class="hljs-keyword">if</span>(count == <span class="hljs-number">3</span>) &#123; <span class="hljs-title function_">alert</span>(count); &#125;<br></code></pre></td></tr></table></figure>
</li>
<li><p>常用输出语句</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">/* 方式1. 警告框 */</span><br><span class="hljs-variable language_">window</span>.<span class="hljs-title function_">alert</span>(<span class="hljs-string">&quot;Hello, JavaScript!&quot;</span>);<br><span class="hljs-comment">/* 方式2. 显示在HTML */</span><br><span class="hljs-variable language_">document</span>.<span class="hljs-title function_">write</span>(<span class="hljs-string">&quot;Hello, JavaScript!&quot;</span>);<br><span class="hljs-comment">/* 方式3. 浏览器控制台 */</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;Hello, JavaScript!&quot;</span>);<br></code></pre></td></tr></table></figure></li>
</ul>
<h3 id="3-2-变量"><a href="#3-2-变量" class="headerlink" title="3.2 变量"></a>3.2 变量</h3><p>JavaScript 中用 <code>var</code> 关键字（<code>variable</code> 的缩写）来声明全局变量</p>
<p>JavaScript 是一门<strong>弱类型语言</strong>，变量可以存放不同类型的值。</p>
<p>变量名需要遵守如下规则：</p>
<ul>
<li>组成字符可以是任务字母、数字、下划线<code>_</code> 或美元符号<code>$</code> </li>
<li>变量第一个字符不能是数字</li>
<li>建议使用驼峰命名，例如：<code>var userAge = 10</code></li>
</ul>
<p>ECMAScript 6 新增了 两个关键字表示不同类型的变量，分别是：</p>
<ul>
<li><code>let</code> ，类似于var，范围更小，只在 let 关键字所在的代码块内有效，且不允许重复声明。</li>
<li><code>const</code>，声明一个只读的常量，一旦声明，常量的值就不能改变。</li>
</ul>
<h3 id="3-3-数据类型"><a href="#3-3-数据类型" class="headerlink" title="3.3 数据类型"></a>3.3 数据类型</h3><p>相关链接：<a target="_blank" rel="noopener" href="https://www.w3school.com.cn/js/js_datatypes.asp">JavaScript 数据类型 (w3school.com.cn)</a></p>
<p>JS 中分为 <strong>原始类型</strong> 和 <strong>引用类型</strong>。</p>
<p><code>注: 使用 typeof 运算符可以获取数据类型。</code></p>
<p><strong>原始类型包括：</strong></p>
<ul>
<li><code>number</code>：数字（整数、小数、NaN(Not a Number)）。</li>
<li><code>string</code>：字符串，单双引号都可以。</li>
<li><code>boolean</code>：布尔。true, false。</li>
<li><code>null</code>： 对象为空。</li>
<li><code>undefined</code>：当声明的变量未初始化时，该变量的默认值是 <code>undefined</code>。</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title function_">alert</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-number">3</span>);       <span class="hljs-comment">// number</span><br><span class="hljs-title function_">alert</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-number">3.14</span>);    <span class="hljs-comment">// number</span><br><span class="hljs-title function_">alert</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-string">&#x27;A&#x27;</span>);     <span class="hljs-comment">// string</span><br><span class="hljs-title function_">alert</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-string">&quot;Hello&quot;</span>); <span class="hljs-comment">// string</span><br><span class="hljs-title function_">alert</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">true</span>);    <span class="hljs-comment">// boolean</span><br><span class="hljs-title function_">alert</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">false</span>);   <span class="hljs-comment">// boolean</span><br><span class="hljs-title function_">alert</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-literal">null</span>);	   <span class="hljs-comment">// object</span><br><span class="hljs-keyword">var</span> a;<br><span class="hljs-title function_">alert</span>(<span class="hljs-keyword">typeof</span> a);		<span class="hljs-comment">// undefined</span><br></code></pre></td></tr></table></figure>



<h3 id="3-4-运算符"><a href="#3-4-运算符" class="headerlink" title="3.4 运算符"></a>3.4 运算符</h3><p>JS支持的运算符有以下五种：</p>
<ol>
<li>算术运算符：<code>+</code>，<code>-</code>，<code>*</code>，<code>/</code>，<code>%</code>，<code>++</code>，<code>--</code></li>
<li>赋值运算符：<code>=</code>,  <code>+=</code>,  <code>-=</code>,  <code>*=</code>, <code>/=</code>, <code>%=</code></li>
<li>比较运算符：<code>&gt;</code>，<code>&lt;</code>，<code>&gt;=</code>，<code>&lt;=</code>，<code>!=</code>，<code>==</code>，<code>===</code></li>
<li>逻辑运算符：<code>&amp;&amp;</code>，<code>||</code>，<code>!</code></li>
<li>三元运算符：条件表达式 <code>? true_value : false_value</code></li>
</ol>
<p>注意 <code>==</code> 与 <code>===</code> 的区别：</p>
<p><code>==</code> 会进行类型转换，<code>===</code> 不会进行类型转换，例如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> age1 = <span class="hljs-number">20</span>;<br><span class="hljs-keyword">var</span> age2 = <span class="hljs-string">&quot;20&quot;</span>;<br><span class="hljs-keyword">var</span> age3 = <span class="hljs-number">20</span>;<br><span class="hljs-title function_">alert</span>(age1 == age2); <span class="hljs-comment">// 结果 true</span><br><span class="hljs-title function_">alert</span>(age1 === age2); <span class="hljs-comment">// 结果 false</span><br><span class="hljs-title function_">alert</span>(age1 === age3); <span class="hljs-comment">// 结果 true</span><br></code></pre></td></tr></table></figure>

<h3 id="3-5-类型转换"><a href="#3-5-类型转换" class="headerlink" title="3.5 类型转换"></a>3.5 类型转换</h3><ul>
<li><p>字符串转为数字：<code>parseInt</code>函数 ，注：若字面值不是数字，则转为 NaN。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title function_">alert</span>(<span class="hljs-built_in">parseInt</span>(<span class="hljs-string">&quot;12&quot;</span>));    <span class="hljs-comment">// 12</span><br><span class="hljs-title function_">alert</span>(<span class="hljs-built_in">parseInt</span>(<span class="hljs-string">&quot;12A15&quot;</span>)); <span class="hljs-comment">// 12</span><br><span class="hljs-title function_">alert</span>(<span class="hljs-built_in">parseInt</span>(<span class="hljs-string">&quot;A15&quot;</span>));   <span class="hljs-comment">// NaN (Not a Number)</span><br></code></pre></td></tr></table></figure>
</li>
<li><p>其他类型转为 布尔类型：<code>if</code>函数</p>
<ul>
<li>Number：0 和 NaN 为 false，其他均转为 true。</li>
<li>String：空字符串为 false，其他均转为 true。</li>
<li>Null 和 undefined 均转为 false。</li>
</ul>
</li>
</ul>
<h2 id="4、JS-函数"><a href="#4、JS-函数" class="headerlink" title="4、JS 函数"></a>4、JS 函数</h2><p>函数（方法）是被设计为执行特定任务的代码块。</p>
<p>定义：JavaScript 函数通过 function 关键字进行定义，语法为：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">function</span> 函数名(参数<span class="hljs-number">1</span>, 参数<span class="hljs-number">2.</span>..)&#123;<br>    <span class="hljs-comment">// 函数要执行的代码  </span><br>    <span class="hljs-keyword">return</span> 返回值<br>&#125;<br></code></pre></td></tr></table></figure>

<p>JS函数的特点：</p>
<ul>
<li>函数接收的参数称为形参，不需要指定类型，因为JS是弱类型语言。</li>
<li>返回值也不需要定义类型，直接 return 返回值即可。</li>
</ul>
<p>调用方式：<code>函数名(实际参数)</code></p>
<h3 id="4-1-函数定义"><a href="#4-1-函数定义" class="headerlink" title="4.1 函数定义"></a>4.1 函数定义</h3><p>JS中函数定义有两种方式（默认在ES6规范下）</p>
<p><strong>第一种：声明函数</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">add</span>(<span class="hljs-params">a, b</span>) &#123; <span class="hljs-keyword">return</span> a + b; &#125;<br><br><span class="hljs-title function_">alert</span>(<span class="hljs-title function_">add</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>));  <span class="hljs-comment">// 结果3</span><br></code></pre></td></tr></table></figure>

<p><strong>第二种：将匿名函数赋值给变量</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> add = <span class="hljs-keyword">function</span>(<span class="hljs-params">a, b</span>)&#123; <span class="hljs-keyword">return</span> a + b; &#125;<br></code></pre></td></tr></table></figure>

<h2 id="5、-JS对象"><a href="#5、-JS对象" class="headerlink" title="5、 JS对象"></a>5、 JS对象</h2><p>相关链接：<a target="_blank" rel="noopener" href="https://www.w3school.com.cn/js/js_objects.asp">JavaScript 对象 (w3school.com.cn)</a></p>
<p>JS中最常用的五大对象有：Array、String、JSON、BOM、DOM</p>
<h3 id="5-1-Array-数组"><a href="#5-1-Array-数组" class="headerlink" title="5.1 Array 数组"></a>5.1 Array 数组</h3><p>JS数组的特点：长度可变，类型可变</p>
<h4 id="定义与使用"><a href="#定义与使用" class="headerlink" title="定义与使用"></a>定义与使用</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 定义数组</span><br><span class="hljs-comment">// 方式1: var 变量名 = new Array(元素列表)</span><br><span class="hljs-keyword">var</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Array</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>);<br><span class="hljs-comment">// 方式2: var 变量名 = [元素列表]</span><br><span class="hljs-keyword">var</span> arr = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>];<br><br><span class="hljs-comment">// 访问数组元素: arr[索引] = 值</span><br>arr[<span class="hljs-number">2</span>] = <span class="hljs-number">4</span>;<br><br><span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.<span class="hljs-property">length</span>; i++)&#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr[i] + <span class="hljs-string">&quot; &quot;</span>);<br>&#125;<span class="hljs-comment">// 结果1 2 4</span><br><br><span class="hljs-comment">// 长度可变, 类型可变</span><br>arr[<span class="hljs-number">4</span>] = <span class="hljs-string">&quot;hello&quot;</span>;  <span class="hljs-comment">// 此时arr数组元素为 1 2 4 undefined hello</span><br>arr[<span class="hljs-number">5</span>] = <span class="hljs-literal">true</span>;	   <span class="hljs-comment">// 此时arr数组元素为 1 2 4 undefined hello true</span><br></code></pre></td></tr></table></figure>

<h4 id="常用属性和方法"><a href="#常用属性和方法" class="headerlink" title="常用属性和方法"></a>常用属性和方法</h4><p>属性：length：返回数组中元素的个数</p>
<p>方法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">/*</span><br><span class="hljs-comment">	forEach()  -- 遍历数组中的每个有值的元素，并调用一次传入的函数</span><br><span class="hljs-comment">	push()     -- 将新元素添加到数组的末尾，并返回新的长度</span><br><span class="hljs-comment">	splice()   -- 从数组中删除元素</span><br><span class="hljs-comment">*/</span><br><span class="hljs-keyword">var</span> arr = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>];<br><br>arr.<span class="hljs-title function_">forEach</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>)&#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(e);<br>&#125;)<br><br><span class="hljs-comment">// ES6特性, 箭头函数(常用)</span><br>arr.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(e);<br>&#125;)<br></code></pre></td></tr></table></figure>





<h3 id="5-2-String-字符串"><a href="#5-2-String-字符串" class="headerlink" title="5.2 String 字符串"></a>5.2 String 字符串</h3><p>相关链接：<a target="_blank" rel="noopener" href="https://www.w3school.com.cn/js/js_strings.asp">JavaScript 字符串 (w3school.com.cn)</a></p>
<h4 id="定义与使用-1"><a href="#定义与使用-1" class="headerlink" title="定义与使用"></a>定义与使用</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 定义字符串</span><br><span class="hljs-comment">// 方式1: var 变量名 = new String(&quot;...&quot;)</span><br><span class="hljs-keyword">var</span> str = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(<span class="hljs-string">&quot;hello&quot;</span>);<br><span class="hljs-comment">// 方式2: var 变量名 = &quot;...&quot;</span><br><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;hello&quot;</span>;<br></code></pre></td></tr></table></figure>

<h4 id="常用属性和方法-1"><a href="#常用属性和方法-1" class="headerlink" title="常用属性和方法"></a>常用属性和方法</h4><p>属性：<code>length</code>, 字符串的长度</p>
<p>方法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">/*</span><br><span class="hljs-comment">	charAt()    -- 返回在指定位置的字符</span><br><span class="hljs-comment">	indexOf()   -- 检索字符串</span><br><span class="hljs-comment">	trim()      -- 去除字符串两边的空格</span><br><span class="hljs-comment">	substring(i, j) -- 提取字符串中两个指定的索引号之间的字符 (含头不含尾)</span><br><span class="hljs-comment">*/</span><br><br><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Hello&quot;</span>;<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(str.<span class="hljs-property">length</span>);  <span class="hljs-comment">// 10</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(str.<span class="hljs-title function_">charAt</span>(<span class="hljs-number">4</span>)); <span class="hljs-comment">// o</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(str.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">&quot;lo&quot;</span>)); <span class="hljs-comment">// 3</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(str.<span class="hljs-title function_">substring</span>(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>)); <span class="hljs-comment">// el</span><br></code></pre></td></tr></table></figure>



<h3 id="5-3-JS自定义对象-JSON"><a href="#5-3-JS自定义对象-JSON" class="headerlink" title="5.3 JS自定义对象 JSON"></a>5.3 JS自定义对象 JSON</h3><p>相关链接：<a target="_blank" rel="noopener" href="https://www.w3school.com.cn/js/js_objects.asp">JavaScript 对象 (w3school.com.cn)</a></p>
<p>JSON（JavaScript Object Notation），即 JavaScript 对象标记法 。</p>
<p>JSON是通过 JavaScript 对象标记法书写的文本。</p>
<p>JSON 特点：</p>
<ul>
<li>语法简单，层次清晰</li>
<li>多用于数据载体，用于前后端交互，在网络中进行数据传输。</li>
</ul>
<h4 id="定义与使用-2"><a href="#定义与使用-2" class="headerlink" title="定义与使用"></a>定义与使用</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">/*</span><br><span class="hljs-comment">	var 变量名 = &#123;&quot;key1&quot;: value1, &quot;key2&quot;: value2&#125;;</span><br><span class="hljs-comment">	    value的数组类型有</span><br><span class="hljs-comment">	      - 数字 (整数或浮点数)</span><br><span class="hljs-comment">	      - 字符串 (在双引号中)</span><br><span class="hljs-comment">	      - 逻辑值 (true 或 false)</span><br><span class="hljs-comment">	      - 数组 (在方括号[]中)</span><br><span class="hljs-comment">	      - 对象 (在花括号&#123;&#125;中)</span><br><span class="hljs-comment">	      - null</span><br><span class="hljs-comment">*/</span><br><br><span class="hljs-keyword">var</span> user = &#123;<br>    name : <span class="hljs-string">&quot;张三&quot;</span>,<br>    <span class="hljs-attr">age</span>: <span class="hljs-number">10</span>,<br>    <span class="hljs-attr">gender</span>: <span class="hljs-string">&quot;male&quot;</span>,<br>    <span class="hljs-attr">eat</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-title function_">alert</span>(<span class="hljs-string">&quot;该吃饭了&quot;</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(user.<span class="hljs-property">name</span>); <span class="hljs-comment">// 张三</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(user.<span class="hljs-property">gender</span>); <span class="hljs-comment">// male</span><br></code></pre></td></tr></table></figure>



<h3 id="5-4-BOM-对象"><a href="#5-4-BOM-对象" class="headerlink" title="5.4 BOM 对象"></a>5.4 BOM 对象</h3><p>相关链接：<a target="_blank" rel="noopener" href="https://www.w3school.com.cn/js/js_window.asp">JavaScript Window - 浏览器对象模型 (w3school.com.cn)</a></p>
<p>BOM（Browser Object Model）浏览器对象模型，允许 JavaScript 与浏览器对话，JavaScript将浏览器的各个组成部分封装为对象。</p>
<p><strong>BOM组成：</strong></p>
<ul>
<li>window：浏览器窗口对象</li>
<li>Navigator：浏览器对象</li>
<li>Screen：屏幕对象</li>
<li>History：历史记录对象</li>
<li>Location：地址栏对象</li>
</ul>
<h4 id="window-对象"><a href="#window-对象" class="headerlink" title="window 对象"></a>window 对象</h4><p>window对象，是 JS中BOM对象里的 window对象，即浏览器窗口对象。</p>
<p><strong>window对象常用属性：</strong></p>
<ul>
<li><code>history</code>：对 History对象的只读引用。</li>
<li><code>location</code>：用于窗口或框架的 Location 对象。</li>
<li><code>navigator</code>：对 Navigator 对象的只读引用。</li>
</ul>
<p><strong>window对象常用方法：</strong></p>
<ul>
<li><code>alert()</code>，警告框</li>
<li><code>confirm()</code>，确认框，有确认和取消按钮</li>
<li><code>setInterval(函数, 毫秒数)</code>，执行器，在指定的周期（单位毫秒）里调用函数或计算表达式。</li>
<li><code>setTimeout(函数, 毫秒数)</code>, 定时器，在指定的毫秒数后调用函数或计算表达式。</li>
</ul>
<p>JS中调用window对象里的方法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-variable language_">window</span>.<span class="hljs-title function_">alert</span>(<span class="hljs-string">&quot;hello&quot;</span>);<br><span class="hljs-comment">// 简写</span><br><span class="hljs-title function_">alert</span>(<span class="hljs-string">&quot;hello&quot;</span>);<br></code></pre></td></tr></table></figure>

<p><code>注: JS调用window对象里的方法，通常可以省略window，直接调用。</code></p>
<p><strong>setInterval执行器用法：</strong></p>
<p>案例：实现3秒倒计时</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> i = <span class="hljs-number">3</span>;<br><span class="hljs-keyword">var</span> taskId = <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>    <span class="hljs-keyword">if</span>(i &lt;= <span class="hljs-number">0</span>) &#123;<br>        <span class="hljs-built_in">clearInterval</span>(taskId);	<span class="hljs-comment">// 退出执行</span><br>    	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;时间到&quot;</span>);<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>    &#125;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(i--);<br>&#125;, <span class="hljs-number">1000</span>)<br></code></pre></td></tr></table></figure>



<p><strong>JS的windows对象中的 Location 对象用法：</strong></p>
<p>Location 对象包括属性：href，设置或返回完整的 URL。</p>
<p>通常可用来实现跳转网站。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript">location.<span class="hljs-property">href</span> = <span class="hljs-string">&quot;https://www.baidu.com&quot;</span>;<br></code></pre></td></tr></table></figure>

<h3 id="5-5-DOM-对象"><a href="#5-5-DOM-对象" class="headerlink" title="5.5 DOM 对象"></a>5.5 DOM 对象</h3><p>相关链接：<a target="_blank" rel="noopener" href="https://www.w3school.com.cn/js/js_htmldom_document.asp">JavaScript HTML DOM 文档 (w3school.com.cn)</a></p>
<p>DOM（Document Object Model），文档对象模型，位于window对象中。</p>
<p>当网页被加载时，浏览器会创建页面的文档对象模型DOM。</p>
<p>HTML DOM 模型可以被结构化为对象树：</p>
<p><img src="https://uni-pic-bed.oss-cn-hangzhou.aliyuncs.com/blog2024/202403051913427.png" srcset="/img/loading.gif" lazyload alt="image-20240305191312205"></p>
<p>将标记语言的各个组成部分封装为对应的对象：</p>
<ul>
<li>Document ：整个文档对象</li>
<li>Element：元素对象</li>
<li>Attribute：属性对象</li>
<li>Text：文本对象</li>
<li>Comment：注释对象</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>DOM<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>DOM 对象标题<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;http://www.baidu.com&quot;</span>&gt;</span>百度<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>JS 可以通过DOM对象对 <strong>HTML标签</strong> 元素进行操作，比如：</p>
<ul>
<li>改变 HTML 元素的内容</li>
<li>改变 HTML 元素的样式（CSS）</li>
<li>对 HTML DOM 事件作出反应</li>
<li>添加和删除 HTML 元素</li>
</ul>
<p>DOM 是 W3C（万维网联盟）的标准，定义了访问 HTML 和 XML 文档的标准，分为3个不同的部分：</p>
<ol>
<li>Core DOM - 所有文档类型的标准模型<ul>
<li>Document：整个文档对象</li>
<li>Element：元素对象</li>
<li>Atrribute：属性对象</li>
<li>Text：文本对象</li>
<li>Comment：注释对象</li>
</ul>
</li>
<li>XML DOM - XML 文档的标准模型</li>
<li>HTML DOM - HTML 文档的标准模型</li>
</ol>
<ul>
<li>Image ：<code>&lt;img&gt;</code></li>
<li>Button：<code>&lt;input type=&quot;button&quot;&gt;</code></li>
</ul>
<p>JS 操作 HTML元素，需要通过 <code>Element 对象</code>。</p>
<p>而HTML 中的 <code>Element 对象</code>是通过 <code>window 对象</code>中的 <code>Document 对象</code>获取的。</p>
<p>Document 对象中提供了一下获取 Element 元素对象的函数：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 1. 根据 id 属性值获取, 返回单个 Element 对象</span><br><span class="hljs-keyword">var</span> h1 = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&quot;h1&quot;</span>);<br><span class="hljs-comment">// 2. 根据标签名称获取, 返回 Element 对象数组</span><br><span class="hljs-keyword">var</span> divs = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementsByTagName</span>(<span class="hljs-string">&quot;div&quot;</span>);<br><span class="hljs-comment">// 3. 根据 name 属性值获取, 返回 Element 对象数组</span><br><span class="hljs-keyword">var</span> hobbys = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementsByName</span>(<span class="hljs-string">&quot;hooby&quot;</span>);<br><span class="hljs-comment">// 4. 根据 class 属性值获取, 返回 Element 对象数组</span><br><span class="hljs-keyword">var</span> cls = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElemntsByClassName</span>(<span class="hljs-string">&quot;cls&quot;</span>);<br></code></pre></td></tr></table></figure>

<p>获取到 Element 对象后，该对象中所含的属性值与 HTML标签对应的属性值相对于，参考链接：<a target="_blank" rel="noopener" href="https://www.w3school.com.cn/js/js_htmldom.asp">JavaScript HTML DOM (w3school.com.cn)</a></p>
<h2 id="6、JS-事件监听"><a href="#6、JS-事件监听" class="headerlink" title="6、JS 事件监听"></a>6、JS 事件监听</h2><p>相关链接：<a target="_blank" rel="noopener" href="https://www.w3school.com.cn/js/js_htmldom_eventlistener.asp">JavaScript HTML DOM 事件监听程序 (w3school.com.cn)</a></p>
<p>这里的事件是指 HTML事件，即发生在 HTML 元素上的事情，比如：</p>
<ul>
<li>按钮被单击</li>
<li>鼠标移动到元素上</li>
<li>按下键盘按键</li>
</ul>
<p><strong>事件监听：</strong> JavaScript 可以在事件被侦测到时 <strong>执行代码</strong>。</p>
<h3 id="6-1-事件绑定"><a href="#6-1-事件绑定" class="headerlink" title="6.1 事件绑定"></a>6.1 事件绑定</h3><p>JS 监听事件前需要进行绑定，JS中有两种方式可实现事件绑定。</p>
<p><strong>方式1：通过 HTML 标签中的事件属性进行绑定。</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;on()&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;按钮1&quot;</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">	<span class="hljs-keyword">function</span> <span class="hljs-title function_">on</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-title function_">alert</span>(<span class="hljs-string">&quot;按钮被点击了&quot;</span>);</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>方式2：通过 DOM 元素属性进行绑定。</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;btn&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;按钮2&quot;</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">	<span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;btn&#x27;</span>).<span class="hljs-property">onclick</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-title function_">alert</span>(<span class="hljs-string">&quot;我被点击了&quot;</span>);</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>




                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>前端基础学习2 | JavaScript</div>
      <div>http://example.com/2024/03/05/前端基础学习2-JavaScript/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2024年3月5日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2024/03/06/%E5%89%8D%E7%AB%AF%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A01-Vue/" title="前端框架学习1 | Vue">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">前端框架学习1 | Vue</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2024/03/05/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E5%AD%A6%E4%B9%A02-JavaScript%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95%E3%80%81%E5%87%BD%E6%95%B0%E3%80%81%E5%AF%B9%E8%B1%A1%E3%80%81%E4%BA%8B%E4%BB%B6%E7%9B%91%E5%90%AC/" title="前端基础学习2 | JavaScript基础语法、函数、对象、事件监听">
                        <span class="hidden-mobile">前端基础学习2 | JavaScript基础语法、函数、对象、事件监听</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>博客网站基于Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>主题Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>





  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  var relativeDate = function() {
    var updatedTime = document.getElementById('updated-time');
    if (updatedTime) {
      var text = updatedTime.textContent;
      var reg = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:Z|[+-]\d{2}:\d{2})/;
      var matchs = text.match(reg);
      if (matchs) {
        var relativeTime = moment(matchs[0]).fromNow();
        updatedTime.textContent = text.replace(reg, relativeTime);
      }
      updatedTime.style.display = '';
    }
  };
  Fluid.utils.createScript('https://lib.baomitu.com/moment.js/2.29.4/moment.min.js', function() {
    if (!'zh-cn'.startsWith('en')) {
      Fluid.utils.createScript('https://lib.baomitu.com/moment.js/2.29.4/locale/zh-cn.min.js', function() {
        relativeDate();
      });
    } else {
      relativeDate();
    }
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
